package com.moxi.mogublog.xo.service.xuan.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moxi.mogublog.commons.entity.xuan.College;
import com.moxi.mogublog.commons.entity.xuan.WeiBoTopicEntity;
import com.moxi.mogublog.xo.global.SQLConf;
import com.moxi.mogublog.xo.mapper.xuan.CollegeMapper;
import com.moxi.mogublog.xo.service.xuan.CollegeService;
import com.moxi.mogublog.commons.vo.college.CollegeVo;
import com.moxi.cloudblog.base.enums.EPublish;
import com.moxi.cloudblog.base.enums.EStatus;
import com.moxi.cloudblog.base.global.BaseSQLConf;
import com.moxi.cloudblog.base.holder.RequestHolder;
import com.moxi.cloudblog.base.serviceImpl.SuperServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class CollegeServiceImpl extends SuperServiceImpl<CollegeMapper, College> implements CollegeService {

    @Autowired
    private CollegeService collegeService;

    @Override
    public IPage<College> getListByCollegeSortUid(String collegeSortUid, Long currentPage, Long pageSize) {
        //分页
        Page<College> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);
        QueryWrapper<College> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        queryWrapper.eq(BaseSQLConf.IS_PUBLISH, EPublish.PUBLISH);
        queryWrapper.eq(SQLConf.COLLEGE_SORT_UID, collegeSortUid);

        //因为首页并不需要显示内容，所以需要排除掉内容字段
        queryWrapper.select(College.class, i -> !i.getProperty().equals(SQLConf.CONTENT));
        IPage<College> pageList = collegeService.page(page, queryWrapper);
//        for(int i = 0 ; i < pageList.getSize() - 1; i++){
//            College college = pageList.getRecords().get(i);
//            System.out.println(college.toString());
//        }


        // 给博客增加标签和分类
        // List<Blog> list = blogService.setTagAndSortAndPictureByBlogList(pageList.getRecords());
        //  pageList.setRecords(list);
        return pageList;
    }

    /**
     * @param collegeVo
     * @return java.lang.String
     * @Description: 发布新的博客并且存储到redis
     * @Author: huangzx
     * @Date: 2024/7/19 12:58
     */
    @Override
    public boolean addCollege(CollegeVo collegeVo) {
        HttpServletRequest request = RequestHolder.getRequest();
        College college = new College();
        college.setAuthor(collegeVo.getAuthor());
        college.setArticlesPart(collegeVo.getArticlesPart());
        college.setTitle(collegeVo.getTitle());
        college.setSummary(collegeVo.getSummary());
        college.setContent(collegeVo.getContent());
        college.setCollegeSortUid(collegeVo.getCollegeSortUid());
//        college.setIsOriginal(collegeVo.get());
        college.setIsPublish(collegeVo.getIsPublish());
        college.setStatus(EStatus.ENABLE);
        college.setOpenComment(collegeVo.getOpenComment());
        try {
            // 数据库插入操作
            return collegeService.save(college); // 成功
        } catch (Exception e) {
            // 异常处理
            log.error("Error adding college: ", e);
            return false; // 失败
        }

    }

    @Override
    public List<College> getHotCollegeList() {
        QueryWrapper<College> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("click_count");
        queryWrapper.last("limit 10");
        return baseMapper.selectList(queryWrapper);
    }


//    private void updateSolrAndRedis(Boolean isSave, Blog blog) {
//        // 保存操作，并且文章已设置发布
//        if (isSave && EPublish.PUBLISH.equals(blog.getIsPublish())) {
//            Map<String, Object> map = new HashMap<>();
//            map.put(SysConf.COMMAND, SysConf.ADD);
//            map.put(SysConf.BLOG_UID, blog.getUid());
//            map.put(SysConf.LEVEL, blog.getLevel());
//            map.put(SysConf.CREATE_TIME, blog.getCreateTime());
//
//            //发送到RabbitMq
//            rabbitTemplate.convertAndSend(SysConf.EXCHANGE_DIRECT, SysConf.MOGU_BLOG, map);
//
//        } else if (EPublish.NO_PUBLISH.equals(blog.getIsPublish())) {
//
//            //这是需要做的是，是删除redis中的该条博客数据
//            Map<String, Object> map = new HashMap<>();
//            map.put(SysConf.COMMAND, SysConf.EDIT);
//            map.put(SysConf.BLOG_UID, blog.getUid());
//            map.put(SysConf.LEVEL, blog.getLevel());
//            map.put(SysConf.CREATE_TIME, blog.getCreateTime());
//
//            //发送到RabbitMq
//            rabbitTemplate.convertAndSend(SysConf.EXCHANGE_DIRECT, SysConf.MOGU_BLOG, map);
//        }
//    }
}
